package com.ktgj.flight.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ktgj.common.core.annotation.Excel;
import com.ktgj.common.core.constant.SalePolicyConstants;
import com.ktgj.common.core.domain.R;
import com.ktgj.customer.api.RemoteCustomerService;
import com.ktgj.customer.api.domain.Employee;
import com.ktgj.flight.domain.Airliner;
import com.ktgj.flight.domain.vo.AddFlightChannelRuleVo;
import com.ktgj.flight.domain.vo.FlightChannelRuleVo;
import com.ktgj.flight.mapper.AirlinerMapper;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.flight.mapper.FlightChannelRuleMapper;
import com.ktgj.flight.domain.FlightChannelRule;
import com.ktgj.flight.service.IFlightChannelRuleService;

import javax.servlet.http.HttpServletRequest;

/**
 * 出票渠道规定主Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-23
 */
@Service
public class FlightChannelRuleServiceImpl implements IFlightChannelRuleService 
{
    @Autowired
    private FlightChannelRuleMapper flightChannelRuleMapper;
    @Autowired
    private AirlinerMapper airlinerMapper;
    @Autowired
    private RemoteCustomerService remoteCustomerService;

    /**
     * 查询出票渠道规定主
     * 
     * @param ruleId 出票渠道规定主ID
     * @return 出票渠道规定主
     */
    @Override
    public FlightChannelRule selectFlightChannelRuleById(Long ruleId)
    {
        return flightChannelRuleMapper.selectFlightChannelRuleById(ruleId);
    }

    /**
     * 查询出票渠道规定主列表
     * 
     * @param flightChannelRule 出票渠道规定主
     * @return 出票渠道规定主
     */
    @Override
    public List<FlightChannelRule> selectFlightChannelRuleList(FlightChannelRule flightChannelRule)
    {
        return flightChannelRuleMapper.selectFlightChannelRuleList(flightChannelRule);
    }

    /**
     * 新增出票渠道规定主
     * 
     * @param flightChannelRule 出票渠道规定主
     * @return 结果
     */
    @Override
    public int insertFlightChannelRule(FlightChannelRule flightChannelRule)
    {
        return flightChannelRuleMapper.insertFlightChannelRule(flightChannelRule);
    }

    /**
     * 修改出票渠道规定主
     * 
     * @param flightChannelRule 出票渠道规定主
     * @return 结果
     */
    @Override
    public int updateFlightChannelRule(FlightChannelRule flightChannelRule)
    {
        return flightChannelRuleMapper.updateFlightChannelRule(flightChannelRule);
    }

    /**
     * 批量删除出票渠道规定主
     * 
     * @param ruleIds 需要删除的出票渠道规定主ID
     * @return 结果
     */
    @Override
    public int deleteFlightChannelRuleByIds(Long[] ruleIds)
    {
        return flightChannelRuleMapper.deleteFlightChannelRuleByIds(ruleIds);
    }

    /**
     * 删除出票渠道规定主信息
     * 
     * @param ruleId 出票渠道规定主ID
     * @return 结果
     */
    @Override
    public int deleteFlightChannelRuleById(Long ruleId)
    {
        return flightChannelRuleMapper.deleteFlightChannelRuleById(ruleId);
    }

    /**
     * 根据航司二字码获取渠道信息
     * @param airliner
     * @return
     */
    @Override
    public FlightChannelRule selectFlightChannelInfo(String airliner) {
        return flightChannelRuleMapper.selectFlightChannelInfo(airliner);
    }

    /**
     * 出票规定列表
     * */
    @Override
    public List<FlightChannelRule> selectFlightChannelRuleBy(FlightChannelRuleVo flightChannelRuleVo) {
        List<FlightChannelRule> flightChannelRules = flightChannelRuleMapper.selectFlightChannelRuleBy(flightChannelRuleVo);

        if (flightChannelRules.size() > 0){
            for (FlightChannelRule flightChannelRule : flightChannelRules) {
                Airliner airliner = airlinerMapper.selectName(flightChannelRule.getAirlinerId());
                if (airliner != null)
                    flightChannelRule.setAirlinerId(airliner.getAirliner());

                if (flightChannelRule.getIsCreatePnr() == 1){
                    flightChannelRule.setPnr("false");
                }
                else if (flightChannelRule.getIsCreatePnr() == 2){
                    flightChannelRule.setPnr("true");
                }
            }
        }
        return flightChannelRules;
    }

    /**
     * 新建出票规定
     * */
    @Override
    public int addFlightChannelRule(HttpServletRequest request, AddFlightChannelRuleVo addFlightChannelRuleVo) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        FlightChannelRule flightChannelRule = new FlightChannelRule();
        flightChannelRule.setAirlinerId(addFlightChannelRuleVo.getAirline());
        flightChannelRule.setPeksourcetype(addFlightChannelRuleVo.getTicketingChannel());
        flightChannelRule.setRemotesourcetype(addFlightChannelRuleVo.getRemoteFlight());

        try {
            flightChannelRule.setStarttime(format.parse(addFlightChannelRuleVo.getStart()));
            flightChannelRule.setEndtime(format.parse(addFlightChannelRuleVo.getFinish()));
        } catch (Exception e){
            throw new Exception("日期转换错误");
        }

        String token = request.getHeader("token");
        R<Employee> loginEmployeeByToken = remoteCustomerService.getLoginEmployeeByToken(token);
        if (loginEmployeeByToken.getData() != null){
            flightChannelRule.setEmployeeid(loginEmployeeByToken.getData().getEmployeeId());
        }
        else {
            throw new Exception("登陆已过期");
        }
        flightChannelRule.setOperationtime(new Date());

        if (addFlightChannelRuleVo.getPnr().equals("true")){
            flightChannelRule.setIsCreatePnr(2l);
        }
        else if (addFlightChannelRuleVo.getPnr().equals("false")){
            flightChannelRule.setIsCreatePnr(1l);
        }
        else {
            throw new Exception("未知参数");
        }
        flightChannelRule.setCabin(addFlightChannelRuleVo.getPosition());
        flightChannelRule.setTicketWay(addFlightChannelRuleVo.getRemoteFlight());

        int i;
        if (addFlightChannelRuleVo.getId() != null && addFlightChannelRuleVo.getId() != 0){
            flightChannelRule.setRuleId(addFlightChannelRuleVo.getId());
            i = flightChannelRuleMapper.updateFlightChannelRule(flightChannelRule);
        }
        else {
            i = flightChannelRuleMapper.insertFlightChannelRule(flightChannelRule);
        }

        return i;
    }
}
